/*
 * ProtocolsKnowledge.java
 *
 * Created on 30 de Agosto de 2006, 00:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.ufal.ic.forbile.infra.interaction;

import br.ufal.ic.forbile.infra.KnowledgeOWL;
import br.ufal.ic.forbile.infra.interaction.ontology.InteractionFactory;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.query.QueryResults;
import java.util.ArrayList;
import java.util.Collection;


/**
 *
 * @author Ig Ibert
 */
public class KnowledgeInteraction implements KnowledgeOWL {
    
    private Collection  errors;
    private OWLModel    owlModel;
    private Project     project;
    private InteractionFactory interaction;
    
    private static KnowledgeInteraction instance;
    private static Integer connection;
    
    /**
     * Creates a new instance of ProtocolsKnowledge
     */
    private KnowledgeInteraction() {
        
    }
    
    private KnowledgeInteraction(final String owlFile) {
        this.errors = new ArrayList();
        
        this.project = Project.loadProjectFromFile(owlFile, this.getErrors());
        this.owlModel = (OWLModel) this.getProject().getKnowledgeBase();
        
        this.interaction = new InteractionFactory(getOwlModel());
    }
    
    public static synchronized KnowledgeInteraction getInstance(final String owlFile){
        if (instance == null) {
            instance = new KnowledgeInteraction(owlFile);
            connection = 1;
        } else {
            connection++;
        }
        return instance;
    }
    
    public Integer getConnection(){
        return this.connection;
    }
    
    public Collection getErrors() {
        return errors;
    }
    
    public OWLModel getOwlModel() {
        return owlModel;
    }
    
    public Project getProject() {
        return project;
    }
    
    public Object getOntologyFactory() {
        return interaction;
    }
    
    public QueryResults executeQuery(final String query) throws Exception{
        return this.owlModel.executeSPARQLQuery(query);
    }
    
    public void save(){
        this.project.save(this.errors);
    }
}